Public Class Form1
Dim hComm As Integer 'COMポート用ハンドル
Dim stDCB As DCB 'DCBインスタンス
Dim timeOut As COMMTIMEOUTS 'タイムアウト用インスタンス
Dim wDATA As String 'COM送信バッファ
Dim rDATA(100) As Byte 'COM受信バッファ
Dim dLen, wLen, rLen As Int32 'COM用パラメータ
Dim bRet As Boolean '関数戻り値
Dim CommName As String 'COMポート番号
Dim Flag As Integer
Dim TB0, TB1, TB2 As String
Dim OutData, OldData As Long
Dim Index, i As Integer
Dim DT0(3000) As Integer
Dim DT1(3000) As Integer
Dim DT2(3000) As Integer
Dim TBoxT0(3000) As Integer
Dim TBoxT1(3000) As Integer
Dim TBoxT2(3000) As Integer
Dim DataT0(3000) As Integer
Dim DataT1(3000) As Integer
Dim DataT2(3000) As Integer
Dim Xd, Yd0, Yd1, Yd2, Xdif, Ydif0, Ydif1, Ydif2 As Single
Dim W As Integer = Width
Dim H As Integer = Height
Private Sub TimeOutSet() 'COM用タイムアウト関数
timeOut.ReadIntervalTimeout = 20
timeOut.ReadTotalTimeoutConstant = 50
timeOut.ReadTotalTimeoutMultiplier = 10
timeOut.WriteTotalTimeoutMultiplier = 10
timeOut.WriteTotalTimeoutConstant = 10
bRet = SetCommTimeouts(hComm, timeOut)
End Sub
'USB接続ボタン
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CommName = TextBox1.Text 'COMポート番号取得
bRet = CloseHandle(hComm)
hComm = CreateFile(CommName, &HC0000000, 0, IntPtr.Zero, &H3, &H80, IntPtr.Zero)
If hComm <> -1 Then
stDCB.BaudRate = 115200 'DCB構造体データ設定
stDCB.fBitFields = &H1
stDCB.ByteSize = 8
stDCB.Parity = 0
stDCB.StopBits = 0
bRet = SetCommState(hComm, stDCB)
TimeOutSet()
If bRet = True Then '接続確認 コマンド「0」の応答「OK」をチェック
Application.DoEvents()
wDATA = Chr(&H30) 'コマンド0 (&H30 = 0) 送信
dLen = 1
bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero)
bRet = ReadFile(hComm, rDATA, 30, rLen, IntPtr.Zero)
If bRet = True And rLen = 3 Then
If rDATA(0) = &H4F And rDATA(1) = &H4B Then ' OKが返信
TextBox2.Text = "Connect" '正常接続完了
Flag = 0
Else
TextBox2.Text = "False" '接続異常
End If
Else
TextBox2.Text = "NoAns" '応答なし
End If
Else
TextBox2.Text = "Fault" '接続不可
End If
Else
TextBox2.Text = "NoExist" 'デバイス見つからず
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Interval = 1100 'タイマー時間設定(1秒+α)
Timer1.Enabled = True 'タイマー スタート
wDATA = Chr(&H31) 'コマンド0x31 (=1) 送信
dLen = Len(wDATA)
bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) 'コマンド送信後
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Timer1.Enabled = False 'タイマー 一時停止
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Me.Refresh()
OutData = 1
OldData = 1
Index = -1
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
End Sub
'一定時間ごとの計測と描画
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim G1 As Graphics = PictureBox2.CreateGraphics
Dim p1 As New Pen(Color.Red)
p1.Width = 2
Dim p2 As New Pen(Color.Green)
p2.Width = 2
Dim p3 As New Pen(Color.Blue)
p3.Width = 2
Xd = W / 5 'X座標ステップの設定
Xdif = Xd / 30 '60 → 30
G1.ScaleTransform(1, -1) 'Y座標の極性反転
Yd0 = H / 7
Ydif0 = Yd0 / 100 ' Y座標のキザミ 10 → 100
Yd1 = H / 7
Ydif1 = Yd1 / 100
Yd2 = H / 7
Ydif2 = Yd2 / 100
G1.TranslateTransform(1, -470) '原点座標の設定 −382 → −470
Index = Index + 1
wDATA = Chr(&H31) 'コマンド0x31 (=1) 送信
dLen = Len(wDATA)
bRet = WriteFile(hComm, wDATA, dLen, wLen, IntPtr.Zero) 'コマンド送信
bRet = ReadFile(hComm, rDATA, 64, rLen, IntPtr.Zero) 'データ受信(前回分)
If bRet = True Then
TB0 = ""
TB1 = ""
TB2 = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
For N = 1 To 4 Step 1
TB0 = TB0 & Chr(rDATA(N))
Next
For N = 7 To 10 Step 1
TB1 = TB1 & Chr(rDATA(N))
Next
For N = 13 To 16 Step 1
TB2 = TB2 & Chr(rDATA(N))
Next
TextBox3.Text = TextBox3.Text & TB0 & Chr(&H20)
TextBox4.Text = TextBox4.Text & TB1 & Chr(&H20)
TextBox5.Text = TextBox5.Text & TB2 & Chr(&H20)
DT0(Index) = Val(TextBox3.Text & Chr(0))
DT1(Index) = Val(TextBox4.Text & Chr(0))
DT2(Index) = Val(TextBox5.Text & Chr(0))
End If
If Index > 0 Then
'折れ線グラフの描画'(* 出来合いの罫線に対する調整箇所A: - 4)
G1.DrawLine(p1, OldData, DT0(Index - 1) * Ydif0 - 4, OutData, DT0(Index) * Ydif0 - 4)
G1.DrawLine(p2, OldData, DT1(Index - 1) * Ydif1 - 4, OutData, DT1(Index) * Ydif1 - 4)
G1.DrawLine(p3, OldData, DT2(Index - 1) * Ydif2 - 4, OutData, DT2(Index) * Ydif2 - 4)
End If
OldData = OutData
OutData = OutData + Xdif
TBoxT0(Index) = DT0(Index)
TBoxT1(Index) = DT1(Index)
TBoxT2(Index) = DT2(Index)
End Sub
'罫線の描画 PictureBox2
Private Sub TempChart(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
Dim Xd, Yd As Integer
Dim i As Integer
e.Graphics.ScaleTransform(1, -1) 'Y座標の極性反転
Xd = W / 5 'X,Y座標ステップの設定
Yd = H / 7
e.Graphics.TranslateTransform(1, -1) '原点座標の設定
e.Graphics.Clear(Color.White) '背景色 白クリア
'横軸
For i = -2 To 10
e.Graphics.DrawLine(Pens.Gray, 1, (i - 9) * Yd, W * 2, (i - 9) * Yd)
Next
e.Graphics.DrawLine(Pens.BlueViolet, 1, 0, W * 2, 0)
'縦軸
For i = 0 To 11
e.Graphics.DrawLine(Pens.Gray, -(i - 10) * Xd, 0, -(i - 10) * Xd, -H * 2)
Next
e.Graphics.DrawLine(Pens.BlueViolet, 1, 0, 1, -H * 2)
e.Graphics.DrawLine(Pens.BlueViolet, 1, -258, H * 2, -258)
e.Graphics.DrawLine(Pens.BlueViolet, 1, -43, H * 2, -43)
End Sub
'データの保存
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim myFile As String = ""
Dim i As Integer
Dim tw As System.IO.TextWriter
SaveFileDialog1.Filter = "csvファイル(*.csv)|*.csv"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myFile = SaveFileDialog1.FileName
End If
If myFile <> "" Then
Try
tw = My.Computer.FileSystem.OpenTextFileWriter(myFile, False, System.Text.Encoding.Default)
For i = 0 To Index - 1
tw.WriteLine(Str(TBoxT0(i))) 'Str: 数値を文字列化,交互に書き込む
tw.WriteLine(Str(TBoxT1(i)))
tw.WriteLine(Str(TBoxT2(i)))
Next
tw.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
'読み出しと再描画
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim myFile As String = ""
Dim tr As System.IO.TextReader
Dim Item, i As Integer
Dim ss As String
Dim G1 As Graphics = PictureBox2.CreateGraphics
Dim p1 As New Pen(Color.Red)
p1.Width = 2
Dim p2 As New Pen(Color.Green)
p2.Width = 2
Dim p3 As New Pen(Color.Blue)
p3.Width = 2
Xd = W / 5 'X座標ステップの設定
Xdif = Xd / 30 '60 → 30
G1.ScaleTransform(1, -1) 'Y座標の極性反転
Yd0 = H / 7
Ydif0 = Yd0 / 100
Yd1 = H / 7
Ydif1 = Yd1 / 100
Yd2 = H / 7
Ydif2 = Yd2 / 100
G1.TranslateTransform(1, -470) '原点座標の設定
OpenFileDialog1.Filter = "csvファイル(*.csv)|*.csv"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myFile = OpenFileDialog1.FileName
Try
tr = My.Computer.FileSystem.OpenTextFileReader(myFile, System.Text.Encoding.Default)
Item = 0
Do
ss = tr.ReadLine()
DataT0(Item) = Val(ss)
ss = tr.ReadLine()
DataT1(Item) = Val(ss)
ss = tr.ReadLine()
DataT2(Item) = Val(ss)
Item = Item + 1
Loop While ss IsNot Nothing
tr.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
OutData = 1
OldData = 1
'折れ線グラフの再描画'
For i = 0 To Item - 1
G1.DrawLine(p1, OldData, DataT0(i) * Ydif0 - 4, OutData, DataT0(i + 1) * Ydif0 - 4)
G1.DrawLine(p2, OldData, DataT1(i) * Ydif1 - 4, OutData, DataT1(i + 1) * Ydif1 - 4)
G1.DrawLine(p3, OldData, DataT2(i) * Ydif2 - 4, OutData, DataT2(i + 1) * Ydif2 - 4)
OldData = OutData
OutData = OutData + Xdif
Next
End If
End Sub
Private Sub Label9_Click(sender As System.Object, e As System.EventArgs) Handles Label9.Click
End Sub
Private Sub Label20_Click(sender As System.Object, e As System.EventArgs)
End Sub
End Class